var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listBack").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight);
	$("#isflag").val("");
	$("#all").addClass("a_hover");
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*window.fcolMinWidth;
	}
	$("#listBack").datagrid({
					url:'/backinfo/getBackInfo',
					title:'返款列表',
					method:'get',
					pagination:true,
					pageList: window.PageSize,
					loadMsg:'加载中......',
					nowrap:true,
					rowStyler:function(index,row){   
						if (row.isback){   
				            return 'background-color:'+window.flagColor;   
				        }
				        if(row.isreject){
				        	return 'background-color:yellow';
				        }
				    },
					columns:[[
					         {field:'ck',checkbox:true},
							 {field:'rowid' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{param}","g");
								 var ref=opt.replace(re,row.id);
								 return ref;
							 }},
							 {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
					         {field:'shopname',title:'门店名称', align:'left',width:100},
							 {field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
					         {field:'dealername',title:'经销商名称', align:'left',width:100},
					         {field:'cmonth',title:'统计月份', align:'left',width:80},
					         {field:'backtotal',title:'返款金额', align:'left',width:100},
					         {field:'counttotal',title:'统计金额', align:'left',width:100},
					         {field:'reason',title:'小于统计原因', align:'left',width:100},
					         {field:'rejreason',title:'驳回原因', align:'left',width:100},
					         {field:'counttime',title:'统计时间', align:'left',width:130,sortable:true,formatter:function(value,row,index){
					        	 var datetime=Common.formatterDate2(value);
					        	 return datetime;
					         }},
					         {field:'backtime',title:'返款时间', align:'left',width:130,sortable:true,formatter:function(value,row,index){
					        	 var datetime=Common.formatterDate2(value);
					        	 return datetime;
					         }},
					         {field:'remark',title:'备注',align:'left',width:120,formatter:function(value, row, index){
					        	 if(value==undefined){
						 				return '';
						 			}
						 			else if(value.length<13){
						 				return value;
						 			}else{
						 				return value.substr(0, 13) + '...';
						 			}
						      }}
					     ]],
				});
	$('#backbeginTime').datetimebox({});  
	$('#backendTime').datetimebox({}); 
	$('#countbeginTime').datetimebox({});  
	$('#countendTime').datetimebox({}); 
	$("#serbutton").click(function(){
		vo.isback=$("#isflag").val();
		vo.dealerid=$("#dealerid").val();
		vo.dealername=$("#dealername").val();
		vo.shopid=$("#shopid").val();
		vo.shopname=$("#shopname").val();
		vo.backbegintime=$('#backbeginTime').datetimebox('getValue');
		vo.backendtime=$('#backendTime').datetimebox('getValue');
		vo.countbegintime=$('#countbeginTime').datetimebox('getValue');
		vo.countendtime=$('#countendTime').datetimebox('getValue');
		$("#listBack").datagrid('load',vo);
	});
	if(opt==''){
		$("#listBack").datagrid('hideColumn','rowid');
	}
	$("#listBack").datagrid({
		toolbar: '#tb'
	});
});
/**
 * 按状态查询
 * @param status
 */
function searchStatus(status){
	var sta='';
	if(status=="all"){
		$("#all").addClass("a_hover");
		removeClass($("#enable"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==false){
		sta=false;
		$("#enable").addClass("a_hover");
		removeClass($("#all"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==true){
		sta=true;
		removeClass($("#all"),"a_hover");
		removeClass($("#enable"),"a_hover");
		$("#disable").addClass("a_hover");
	}
	$("#isflag").val(sta);
	vo.isback=sta;
	$("#listBack").datagrid('load',vo);
}


/**
 * 查看
 */
function viewBack(id){
	window.parent.openDialog("返款详情",'/backinfo/viewBackInfo','',420,600,'id='+id);
}

/**
 * 编辑
 * @param id
 */
function edit(id){
	window.parent.openDialog("编辑返款信息",'/backinfo/editbackinfo','',400,500,'id='+id);
}
/**
 * 批量设置返款
 */
function betchBack(){
	var rows=$("#listBack").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('返款','是否按照统计金额进行返款？',function(data){
		if(data){
			var ids='';
			var isback=true;
			for(var i=0;i<rows.length;i++){
				if(rows[i].isback){
					$.messager.alert('错误','存在已返款记录,经销商为:'+rows[i].dealername,'error');
					isback=false;
					break;
				}
				ids+=rows[i].id+',';
			}
			if(!isback){
				return;
			}
			$.ajax({
				type:'post',
				url: '/backinfo/betchBack',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","返款成功",'info',function(){
							$("#listBack").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","返款失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","返款失败：",'error');
				}
			});
		}
	})
}
/**
 * 驳回
 * @param id
 */
function reject(id){
	if(id==''){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.prompt('驳回','请输入驳回原因？',function(data){
		if(data==''){
			$.messager.alert("失败","驳回原因不可为空",'error');
			return;
		}
		if(data){
			$.ajax({
				type:'post',
				url: '/backinfo/reject',
				data:{id:id,rejreason:data},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","驳回成功",'info',function(){
							$("#listBack").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","驳回失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","驳回失败：",'error');
				}
			});
		}
	});
}
/**
 * 批量返款
 */
function betchReject(){
	var rows=$("#listBack").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.prompt('驳回','请输入驳回原因？',function(data){
		if(data==''){
			$.messager.alert("失败","驳回原因不可为空",'error');
			return;
		}
		if(data){
			var ids='';
			var isback=true;
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].id+',';
			}
			$.ajax({
				type:'post',
				url: '/backinfo/betchReject',
				data:{ids:ids,rejreason:data},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","驳回成功",'info',function(){
							$("#listBack").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","驳回失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","驳回失败：",'error');
				}
			});
		}
	});
}
/**
 * 导出为excel
 */
function exportXls(){
	vo.isback=getVal(vo.isback);
	vo.dealerid=getVal(vo.dealerid);
	vo.dealername=getVal(vo.dealername);
	vo.shopid=getVal(vo.shopid);
	vo.shopname=getVal(vo.shopname);
	vo.backbegintime=getVal(vo.backbegintime);
	vo.backendtime=getVal(vo.backendtime);
	vo.countbegintime=getVal(vo.countbegintime);
	vo.countendtime=getVal(vo.countendtime);
	var url='/backinfo/exportXls?isback='+vo.isback+'&dealerid='+vo.dealerid+'&dealername='+
	vo.dealername+'&backbegintime='+vo.backbegintime+'&backendtime='+vo.backendtime+
	'&countbegintime='+vo.countbegintime+'&countendtime='+vo.countendtime+'&shopid='+vo.shopid+'&shopname='+vo.shopname;
	window.open(url,'','',false);
}
